package com.xia.service.impl;

import com.xia.dao.article.ICollectDao;
import com.xia.domian.Collect;
import com.xia.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ICollectServiceImpl implements ICollectService {
    private ICollectDao collectDao;
    @Resource
    private String getDate;
    @Autowired
    public void setCollectDao(ICollectDao collectDao) {
        this.collectDao = collectDao;
    }
    @Override
    public List<Collect> insertCollectArticle(Collect collect) {
        Integer middleArticleId=collect.getCollectArticle();
        if (collect.getCollectTarget() == null){
            collect.setCollectArticle(null);
        }
        collect.setCollectCreatingTime(getDate);
        collectDao.insertCollect(collect);
        return collectArticle(collect.getCollectUsers(),middleArticleId);
    }

    @Override
    public List<Collect> collectArticle(Integer usersId, Integer articleId) {
        List<Collect> collectList=collectDao.selectCollect(usersId);
        //查询当前用户是否有收藏夹
        if (collectList == null){
            return null;
        }
        //查询用户的每个收藏夹中是否有该帖子/文章,有则将字段collectState=1,否则collectState=0
        for (Collect collect:collectList){
            String state=collectDao.selectCollectArticleOne(articleId,collect.getCollectId());
            if (state == null){
                collect.setCollectState(0);
            }else {
                collect.setCollectState(1);
            }
        }
        return collectList;
    }
}
